package com.sinosoft.cloud.access.abc.service;

import com.sinosoft.cloud.access.annotations.AccessService;
import com.sinosoft.cloud.access.service.AccessServiceBean;
import com.sinosoft.cloud.access.util.TimeUtil;
import com.sinosoft.cloud.cbs.fin.client.CancelPayService;
import com.sinosoft.cloud.cbs.pol.CancelService;
import com.sinosoft.cloud.rest.TradeInfo;
import feign.RetryableException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created by zhouqifen on 2017-9-15.
 * 取消交易
 */
@AccessService(name = "abc", transCode = "1009")
public class BusinessCancelService extends AccessServiceBean {

    /**
     * 日志管理
     */
    private final Log cLogger = LogFactory.getLog(this.getClass());

    /**
     * 财务取消交易微服务
     */
    @Autowired
    private CancelPayService cancelPayService;

    /**
     * 取消交易微服务接口
     */
    @Autowired
    private CancelService cancelService;

    @Override
    public TradeInfo service(TradeInfo reqTradeInfo) {

        //错误信息
        String errorStr = "";
        String errorLog = "";
        /**
         *调用微服务接口
         */
        TradeInfo respTradeInfo = null;
        long microServiceStart = 0L;
        long microServiceEnd = 0L;

        try {
            cLogger.info("1.开始调用财务取消交易微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = cancelPayService.service(reqTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "财务取消交易微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
           // errorStr = "调用财务取消交易微服务异常。";
            //cLogger.error(e);
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "财务取消交易", e);
            return getErrorTradeInfo(errorStr);
        }catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "财务取消交易", e);
            return getErrorTradeInfo(errorStr);
        }

        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用财务取消交易微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("财务取消交易微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }

        try {
            cLogger.info("2.开始调用取消交易微服务");
            microServiceStart = System.currentTimeMillis();
            respTradeInfo = cancelService.service(respTradeInfo);
            microServiceEnd = System.currentTimeMillis();
            cLogger.info(TimeUtil.getMillis(microServiceStart, microServiceEnd, "取消交易微服务"));
        } catch (RetryableException e) {
            microServiceEnd = System.currentTimeMillis();
            errorStr = getExceptionStr(microServiceStart, microServiceEnd, "财务取消交易", e);
            return getErrorTradeInfo(errorStr);
        }catch (Exception e) {
            errorStr = getExceptionStr(0L, 0L, "取消交易微服务单号", e);
            return getErrorTradeInfo(errorStr);
        }

        if (respTradeInfo.getErrorList().size() == 0) {
            cLogger.info("调用取消交易微服务成功。返回结果是：" + respTradeInfo.toString());
        } else {
            errorStr = respTradeInfo.getErrorList().get(0).toString();
            errorLog = new StringBuffer(1024).append("取消交易微服务内部发生错误，")
                    .append(respTradeInfo.getErrorList().toString()).toString();
            return getCheckRuleFailInfo(errorStr, errorLog);
        }
        return respTradeInfo;
    }

}

